package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.respository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public Travel get(String id) {

        Optional<Travel> op = travelRepository.findById(id);
        if (op.isPresent()) {
            Travel travel = op.get();
            travel.setAuthor(userInfoService.get(travel.getUserId()));

            travel.setDest(destinationService.get(travel.getDestId()));
            return travel;
        }

        return travelRepository.findById(id).orElse(null);
    }

    @Override
    public List<Travel> list() {
        return travelRepository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {

        // MQL拼接语句
        Query query = new Query();
        System.out.println("游记查询条件="+qo);

        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        // 查询天数
        //TravelCondition day = TravelCondition.DAY_MAP.get(qo.getDayType());
        TravelCondition day = qo.getDay();
        System.out.println("查询天数" + day);
        if (day != null) {
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }

        // 人均消费
        TravelCondition per = qo.getPerExpend();
        System.out.println("人均消费" + per);
        if (per != null) {
            query.addCriteria(Criteria.where("perExpend").gte(per.getMin()).lte(per.getMax()));
        }

        // 只查询公开的游记
        query.addCriteria(Criteria.where("isPublic").is(Travel.ISPUBLIC_YES));


        // 分页对象 传入分页需要的参数 当前页-1  每页行数  排序规则  排序依据
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        // 排序规则
        if (qo.getOrderType() == TravelQuery.ORDER_HOT) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "viewnum");

        } else if (qo.getOrderType() == TravelQuery.ORDER_NEW) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        }


        // 返回分页对象
        // Page 是接口
        // PageImpl 是实现类
        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);
        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {



        // 目的地名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        // 创建时间
        travel.setCreateTime(new Date());
        // 最后更新时间
        travel.setLastUpdateTime(new Date());

        if (StringUtils.hasText(travel.getId())) {

            this.update(travel);
        } else {
            this.save(travel);
        }

    }

    @Override
    public void audit(String id, int state) {

        // 判断是否满足审核条件
        Travel travel = this.get(id);
        if (travel == null || travel.getState() != Travel.STATE_WAITING) {
            throw new LogicException("审核参数异常!");
        }

        if (state == Travel.STATE_RELEASE) {
            // 审核通过之后要做什么
            travel.setState(Travel.STATE_RELEASE);
            travel.setReleaseTime(new Date());
            travel.setLastUpdateTime(new Date());
        } else {
            // 审核拒绝之后要做什么
            travel.setState(Travel.STATE_REJECT);
            travel.setReleaseTime(null);
            travel.setLastUpdateTime(new Date());
        }

        this.update(travel);

    }

    @Override
    public void ban(String id) {
        // 判断是否满足审核条件
        Travel travel = this.get(id);
        if (travel == null) {
            throw new LogicException("下架参数异常!");
        }

        // 将游记状态改为审核中
        travel.setState(Travel.STATE_WAITING);

        this.update(travel);
    }

    @Override
    public List<Travel> queryViewnumTop3(String destId) {
        // 点击量排序
        // 前三 分页 1页 3个
        // 条件 目的地id
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        return travelRepository.findByDestId(destId, pageable);

    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        Query query = new Query();

        query.addCriteria(Criteria.where("_id").is(vo.getTravelId()));

        Update update = new Update();
        update.set("viewnum", vo.getViewnum());
        update.set("favornum", vo.getFavornum());
        update.set("replynum", vo.getReplynum());
        update.set("thumbsupnum", vo.getThumbsupnum());
        update.set("sharenum", vo.getSharenum());

        template.updateMulti(query, update, Travel.class);
    }

    @Override
    public List<Travel> queryByDestId(String id) {
        List<Travel> list = travelRepository.queryByDestId(id);

        for (Travel travel : list) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return list;
    }
}


































































